//
//  advanceSearch.m
//  EGA_Archives_iOS
//
//  Created by gad on 2/14/56 BE.
//  Copyright (c) 2556 EGA. All rights reserved.
//

#import "advanceSearch.h"
#import "popOverDatePicker.h"

#import <QuartzCore/QuartzCore.h>
@interface advanceSearch ()
{
       UIView *popoverView;
    popOverDatePicker *dateController;
    UIDatePicker *datePicker;
       UIPopoverController *popoverController;
     NSString *date;
}

@end

@implementation advanceSearch
@synthesize advanceDelegate = _advanceDelegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    dateController = [[popOverDatePicker alloc] initWithNibName:@"popOverDatePicker" bundle:nil];
    popoverView = [[UIView alloc] init];   //view
    popoverView.backgroundColor = [UIColor blackColor];
    
    datePicker=[[UIDatePicker alloc]init];//Date picker
    // datePicker.frame=CGRectMake(0,44,320, 216);
    datePicker.datePickerMode =UIDatePickerModeDate;
    
    //[datePicker setMinuteInterval:5];
    // [datePicker setTag:10];
    
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    [popoverView addSubview:datePicker];
    
    
    popoverController = [[UIPopoverController alloc] initWithContentViewController:dateController];
    [popoverController setPopoverContentSize:CGSizeMake(320, 216) animated:NO];
    dateController.view=popoverView;
    
    [dateController release];
    
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
   // [dateFormat setDateFormat:@"MM/dd/YYYY"];
    [dateFormat setDateFormat:@"YYYY-MM-dd"];
    if ([self.startDate isEqualToString:NULL]) {
        
        self.texStartDate.text= [dateFormat stringFromDate:self.startDate];
        self.texEndDate.text= [dateFormat stringFromDate:self.endDate];
    }
    else{
        
        NSDate *now = [[NSDate alloc] init];
        self.texStartDate.text= [dateFormat stringFromDate:now];
        self.texEndDate.text= [dateFormat stringFromDate:now];
        
    }
    self.texStartDate.tag=0;
    self.texEndDate.tag=1;
    self.view.layer.borderColor = [UIColor grayColor].CGColor;
    self.view.layer.borderWidth = 2.0f;
    
    UIImage *image = [UIImage imageNamed:@"checkBoxChecked.png"];
    [self.checkButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.checkButton  setBackgroundImage:image forState:UIControlStateSelected];
    [self.checkButton  setBackgroundImage:image forState:UIControlStateHighlighted];
    self.checkButton .adjustsImageWhenHighlighted = YES;
    self.checkButton .selected = NO;

}
- (IBAction)dateChanged:(id)sender {
    //  NSLog(@"dateChanged");
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    
    
    
         if ([date isEqualToString:@"0"]) 
        self.texStartDate.text= [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:datePicker.date]];
    else
        self.texEndDate.text= [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:datePicker.date]];
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)advanchSearch{

    
}

- (IBAction)close:(id)sender {
    if (_advanceDelegate != nil) {
        
        [_advanceDelegate close];
        
    }
}

- (IBAction)advanceSearch:(id)sender {
    NSString *IsDueDate;
    if (_advanceDelegate != nil) {
        if(self.checkButton.selected==NO)
            IsDueDate=@"0";
        else
            IsDueDate=@"1";
      //  [_advanceDelegate advanceSearch:@"testecms2.0":@"2013-01-16":@"2013-01-16":@"0"];
        
    NSString * searchTextUTF8 = [self.textSearch.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [_advanceDelegate advanceSearch:searchTextUTF8:self.texStartDate.text:self.texEndDate.text:IsDueDate];
    }

}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    CGRect popRect ;
    if ([textField isEqual:self.texStartDate]||[textField isEqual:self.texEndDate]) {
        if ([popoverController isPopoverVisible]) {
            [popoverController dismissPopoverAnimated:YES];
        } else {
            switch (textField.tag)
            {
                case 0:
                    
                    popRect = CGRectMake(self.texStartDate.frame.origin.x-20,
                                         self.texStartDate.frame.origin.y,
                                         self.texStartDate.frame.size.width,
                                         self.texStartDate.frame.size.height);
                    date=@"0";
                    
                    
                    
                    break;
                case  1:
                    
                    popRect = CGRectMake(self.texEndDate.frame.origin.x-20,
                                         self.texEndDate.frame.origin.y,
                                         self.texEndDate.frame.size.width,
                                         self.texEndDate.frame.size.height);
                    date=@"1";
                    
                    
                    break;
                default:
                    
                    break;
                    // remainder of switch statement....
            }
            [popoverController presentPopoverFromRect:popRect
                                               inView:self.view
                             permittedArrowDirections:UIPopoverArrowDirectionAny
                                             animated:YES];
            
            
        }
        //        [popoverView release];
        //        [popoverController release];
        
        return NO;
    }
    
    //    NSLog(@"begint");
    return YES;
    
}

- (void)dealloc {
    
 
    [_lblSubmit release];
 
   
    [_texStartDate release];
    [_endDate release];
    [_texEndDate release];
    [_checkButton release];
    [_search release];
    [_textSearch release];
    [super dealloc];
}
-(IBAction)popOverPicker:(id)sender   {
    
    if ([popoverController isPopoverVisible]) {
        [popoverController dismissPopoverAnimated:YES];
    } else {
        //the rectangle here is the frame of the object that presents the popover,
        //in this case, the UIButton…
        CGRect popRect = CGRectMake(self.lblSubmit.frame.origin.x,
                                    self.lblSubmit.frame.origin.y,
                                    self.lblSubmit.frame.size.width,
                                    self.lblSubmit.frame.size.height);
        [popoverController presentPopoverFromRect:popRect
                                           inView:self.view
                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                         animated:YES];
    }
}
- (IBAction)checkAction:(id)sender {
    if(self.checkButton.selected == NO){
        UIImage *image = [UIImage imageNamed:@"checkBoxUnchecked.png"];
        [self.checkButton setBackgroundImage:image forState:UIControlStateNormal];
        [self.checkButton setBackgroundImage:image forState:UIControlStateSelected];
        [self.checkButton setBackgroundImage:image forState:UIControlStateHighlighted];
        self.checkButton.adjustsImageWhenHighlighted = YES;
        self.checkButton.selected = YES;
        NSLog(@"Checked");
    }else{
        UIImage *image = [UIImage imageNamed:@"checkBoxChecked.png"];
        [self.checkButton setBackgroundImage:image forState:UIControlStateNormal];
        [self.checkButton setBackgroundImage:image forState:UIControlStateSelected];
        [self.checkButton setBackgroundImage:image forState:UIControlStateHighlighted];
        self.checkButton.adjustsImageWhenHighlighted = YES;
        self.checkButton.selected = NO;
        NSLog(@"Unchecked");
    }

}
@end
